<?php

class Config
{

    private static $instance;
    private $configs = array();

    private function __construct()
    {
        
    }

    public static function getInstance()
    {

        if (empty(self::$instance))
        {
            self::$instance = new self();
        }

        return self::$instance;

    }

    public static function append($key, $value)
    {
        $target = self::get($key);

        if (!$target)
        {

            self::set($key, $value);
        } else
        {

            if (is_string($target) && is_string($value))
            {
                $target = $target . $value;
            } else if (is_array($target) && is_array($value))
            {
                $target = array_merge($target, $value);
            } else if (is_array($target) && is_string($value))
            {
                $target[] = $value;
            } else
            {
                return false;
            }

            self::set($key, $target);
        }

    }

    public static function add($key, $value)
    {
        $target = self::get($key);

        if (!$target)
        {
            self::set($key, $value);
        } else
        {
            if (is_array($target))
            {
                if (is_array($value))
                {
                    $target = array_merge($value, $target);
                } else
                {
                    $target[] = $value;
                }

                self::set($key, $target);
            } else
            {
                return false;
            }
        }

    }

    public static function set($key, $value)
    {

        if (strpos($key, '.') !== FALSE)
        {
            $keyArray = explode('.', $key);

            self::getInstance()->configs[$keyArray[0]][$keyArray[1]] = $value;
        } else
        {
            self::getInstance()->configs[$key] = $value;
        }

    }

    public static function get($key)
    {
        $instance = self::getInstance();
        if (strpos($key, '.') !== FALSE)
        {

            $keyArray = explode('.', $key);

            if (!empty($instance->configs[$keyArray[0]]))
            {
                if (is_array(self::getInstance()->configs[$keyArray[0]]))
                {

                    $requiredArray = self::getInstance()->configs[$keyArray[0]];

                    if (isset($requiredArray[$keyArray[1]]))
                    {
                        return $requiredArray[$keyArray[1]];
                    }

                    return FALSE;
                } else
                {
                    return FALSE;
                }
            } else
            {
                return FALSE;
            }
        } else
        {

            if (!empty(self::getInstance()->configs[$key]))
            {
                return self::getInstance()->configs[$key];
            } else
            {
                return FALSE;
            }
        }

    }

    public function init($config)
    {
        $this->configs = $config;

    }

    public static function getAssets()
    {
        $assetsString = '';


        if (self::getInstance()->get('assets.css') && is_array(self::getInstance()->get('assets.css')))
        {
            $assetsString .= implode(' ', self::getInstance()->get('assets.css'));
        }

        if (self::getInstance()->get('assets.js') && is_array(self::getInstance()->get('assets.js')))
        {
            $assetsString .= implode(' ', self::getInstance()->get('assets.js'));
        }

        return $assetsString;

    }

    public static function getMeta()
    {
        $assetsString = '';

        if (self::getInstance()->get('assets.extras') && is_array(self::getInstance()->get('assets.extras')))
        {
            $assetsString .= implode(' ', self::getInstance()->get('assets.extras'));
        }

        return $assetsString;

    }

}

